<html>
<head>
  <title>Gerrit Code Review - Releases</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
  <style>
  #diffy_logo {
    float: left;
    width: 75px;
    height: 70px;
    margin-right: 20px;
  }
  #download_container table {
    border-spacing: 0;
  }
  #download_container td {
    padding-right: 5px;
  }
  .latest-release {
    background-color: lightgreen;
  }
  .rc {
    padding-left: 1em;
    font-style: italic;
  }
  .size {
    text-align: right;
  }
  </style>
</head>
<body>

<h1>Gerrit Code Review - Releases</h1>
<a href="https://www.gerritcodereview.com/">
  <img id="diffy_logo" src="" />
</a>

<div id='download_container'>
</div>

<script>
$.getJSON(
'https://www.googleapis.com/storage/v1/b/gerrit-releases/o?projection=noAcl&fields=items(name%2Csize)&callback=?',
function(data) {
  var doc = document;
  var frg = doc.createDocumentFragment();
  var rx = /^gerrit(?:-full)?-([0-9.]+(?:-rc[0-9]+)?)[.]war/;
  var dl = 'https://www.gerritcodereview.com/download/';
  var docs = 'https://gerrit-documentation.storage.googleapis.com/';
  var src = 'https://gerrit.googlesource.com/gerrit/+/'

  var items = data.items.filter(function(i) {
    return i.name.indexOf('gerrit-snapshot-') != 0;
  });


  items.sort(function(a,b) {
    var av = rx.exec(a.name);
    var bv = rx.exec(b.name);
    if (!av || !bv) {
      return a.name > b.name ? 1 : -1;
    }

    var an = av[1].replace('-rc', '.rc').split('.')
    var bn = bv[1].replace('-rc', '.rc').split('.')
    while (an.length < bn.length) an.push('0');
    while (an.length > bn.length) bn.push('0');
    for (var i = 0; i < an.length; i++) {
      var ai = an[i].indexOf('rc') == 0
        ? parseInt(an[i].substring(2))
        : 1000 + parseInt(an[i]);

      var bi = bn[i].indexOf('rc') == 0
        ? parseInt(bn[i].substring(2))
        : 1000 + parseInt(bn[i]);

      if (ai != bi) {
        return ai > bi ? -1 : 1;
      }
    }
    return 0;
  });

  var latest = false;
  for (var i = 0; i < items.length; i++) {
    var f = items[i];
    var v = rx.exec(f.name);

    if ('index.html' == f.name) {
      continue;
    }

    var tr = doc.createElement('tr');
    var td = doc.createElement('td');
    var a = doc.createElement('a');
    a.href = dl + f.name;
    if (v) {
      a.appendChild(doc.createTextNode('Gerrit ' + v[1]));
    } else {
      a.appendChild(doc.createTextNode(f.name));
    }
    if (f.name.indexOf('-rc') > 0) {
      td.className = 'rc';
    } else if (!latest) {
      latest = true;
      tr.className='latest-release';
    }
    td.appendChild(a);
    tr.appendChild(td);

    td = doc.createElement('td');
    td.className = 'size';
    if (f.size/(1024*1024) < 1) {
      sizeText = Math.round(f.size/1024*10)/10 + ' KiB';
    } else {
      sizeText = Math.round(f.size/(1024*1024)*10)/10 + ' MiB';
    }
    td.appendChild(doc.createTextNode(sizeText));
    tr.appendChild(td);

    td_rel = doc.createElement('td');
    td_doc = doc.createElement('td');
    if (v && f.name.indexOf('-rc') < 0) {
      // Release notes link
      a = doc.createElement('a');
      a.href = docs + 'ReleaseNotes/ReleaseNotes-' + v[1] + '.html';
      a.appendChild(doc.createTextNode('Release Notes'));
      td_rel.appendChild(a);

      // Documentation link
      a = doc.createElement('a');
      a.href = docs + 'Documentation/' + v[1] + '/index.html';
      a.appendChild(doc.createTextNode('Documentation'));
      td_doc.appendChild(a);
    }
    tr.appendChild(td_rel);
    tr.appendChild(td_doc);

    td = doc.createElement('td');
    if (v) {
      a = doc.createElement('a');
      a.href = src + 'v' + v[1];
      a.appendChild(doc.createTextNode('src'));
      td.appendChild(a);
    }
    tr.appendChild(td);

    frg.appendChild(tr);
  }

  var tr = doc.createElement('tr');
  var th = doc.createElement('th');
  th.appendChild(doc.createTextNode('File'));
  tr.appendChild(th);

  th = doc.createElement('th');
  th.appendChild(doc.createTextNode('Size'));
  tr.appendChild(th);

  tr.appendChild(doc.createElement('th'));
  tr.appendChild(doc.createElement('th'));

  var table = doc.createElement('table');
  table.appendChild(tr);
  table.appendChild(frg);
  doc.getElementById('download_container').appendChild(table);
});
</script>

</body>
</html>
